//
//  SecondViewController.m
//  UI_豆瓣影院
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "SecondTableViewCell.h"
#import "CinemaModel.h"
#import "UIImageView+WebCache.h"
#import "MyCollectionViewCell.h"
#import "Second1ViewController.h"
#import "AAALHXDateHandle.h"
@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UICollectionView *collect;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowlayOut;
@property (nonatomic, retain) UIBarButtonItem *button;
@property (nonatomic, assign) BOOL b;

@end

@implementation SecondViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"] forBarMetrics:UIBarMetricsDefault];
    }
    return self;
    
}
- (void)dealloc {
    [_tableView release];
    [_arr release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStylePlain] autorelease];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    self.flowlayOut = [[UICollectionViewFlowLayout alloc] init];
    self.collect = [[[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:_flowlayOut] autorelease];
    self.button = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_collection@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    self.navigationItem.rightBarButtonItem = _button;
    
    [self dataHounde];
}

- (void)createTabelView {
    
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"cell"];
    //    [_tableView release];
    UIBarButtonItem * button = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_collection@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    self.navigationItem.rightBarButtonItem = button;
}

- (void)buttonAction:(UIBarButtonItem *)button {
    if (!_b) {
        button.image = [[UIImage imageNamed:@"btn_nav_list@2x.png"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
        
        [self createCollectionView];
        _b = YES;
    } else if (_b) {
        button.image = [UIImage imageNamed:@"btn_nav_collection@2x.png"];
        
        [self createTabelView];
        _b = NO;
    }
    
}

- (void)createCollectionView {
    _flowlayOut.itemSize = CGSizeMake(110, 140);
    _flowlayOut.minimumInteritemSpacing = 5;
    _flowlayOut.minimumLineSpacing = 5;
    _flowlayOut.sectionInset = UIEdgeInsetsMake(10, 10, 100, 10);
    _flowlayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collect.delegate = self;
    _collect.dataSource = self;
    [_collect registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collect.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_collect];
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _arr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    CinemaModel *model = _arr[indexPath.row];
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.image]];
    cell.lable.textAlignment = NSTextAlignmentCenter;
    cell.lable.font = [UIFont boldSystemFontOfSize:20];
    cell.lable.text = model.title;
    
    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // 属性传值
    Second1ViewController *vc = [[Second1ViewController alloc] init];
    
    vc.model = _arr[indexPath.row];
 
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 属性传值
    Second1ViewController *vc = [[Second1ViewController alloc] init];
   
    vc.model = _arr[indexPath.row];
   
    [self.navigationController pushViewController:vc animated:YES];
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arr.count;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 220;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.imageview.image = [UIImage imageNamed:@"bg_eventlistcell@2x.png"];
    CinemaModel *model = _arr[indexPath.row];
    cell.lable.font = [UIFont boldSystemFontOfSize:25];
    cell.lable.text = model.title;
//    [cell.imageview1 sd_setImageWithURL:[NSURL URLWithString:model.image]];
    [cell.imageview1 sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"IMG_4.JPG"]];
    return cell;
    
}
- (void)dataHounde {
    NSString *str = @"http://api.douban.com/v2/movie/nowplaying?app_name=doubanmovie&client=e:iPhone4,1|y:iPhoneOS_6.1|s:mobile|f:doubanmovie_2|v:3.3.1|m:PP_market|udid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&alt=json&version=2&start=1&city=北京&apikey=0df993c66c0c636e29ecbb5344252a4a";
        NSString *newStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:newStr]];
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    NSMutableDictionary *dic1 = result;
    NSArray *arr = dic1[@"entries"];
    self.arr = [NSMutableArray array];
    for (NSMutableDictionary *dic in arr) {
        CinemaModel *model = [CinemaModel cinemaModel];
        [model setValuesForKeysWithDictionary:dic];
        [_arr addObject:model];
        
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
